<?php
/*
 * APPLICATION CLASS
 * @autor	Albert Moreno Vendrell
 */
class Application extends Core {
    private $_Url;
    private $_TimeCreated;
    private $_TimeStarted;
    private $_TimeUpdated;
    private $_TimePaused;
    private $_TimeStoped;
    private $_TimeRestarted;
    private $_TimeDestroyed;

    /********************************
     ************ STATUS ************
     * - CREATING 	-> CREATED
     * - STARTING 	-> STARTED
     * - RUNING
     * - PAUSING 	-> PAUSED
     * - STOPING 	-> STOPED
     * - DESTROYING -> DESTROY
     ********************************
     ********************************/
    private $_Status;
	private $_instance;
	private $_CrashControl 	= 0;
	private $_MaxCrash;

	public function __construct($application_name){
		$configuration      = Configuration::getInstance();
        $this->_MaxCrash 	= $configuration->MaxCrash();

		$this->_instance 	= new $application_name;
		$this->onCreate();
  	}

    public function getUrl(){
        $var=$this->_Url;
        return $var;
    }

    public function getStatus(){
        $var=$this->_Status;
        return $var;
    }

    public function getInstance(){
        $var=$this->_instance;
        return $var;
    }

    public function getTimeCreated(){
        $var=$this->_TimeCreated;
        return $var;
    }

    public function getTimeStarted(){
        $var=$this->_TimeStarted;
        return $var;
    }

    public function getTimeUpdated(){
        $var=$this->_TimeUpdated;
        return $var;
    }

    public function getTimePaused(){
        $var=$this->_TimePaused;
        return $var;
    }

    public function getTimeStoped(){
        $var=$this->_TimeStoped;
        return $var;
    }

    public function getTimeRestarted(){
        $var=$this->_TimeRestarted;
        return $var;
    }

    public function getTimeDestroyed(){
        $var=$this->_TimeDestroyed;
        return $var;
    }

    /*
     * Open application
     * @autor                           Albert Moreno Vendrell
     */
    public function openApplication(){
        switch($_Status){
        	case "CREATING":
		    case "STARTING":
		    case "PAUSING":
		    case "SPTOPING":
		    case "DESTROYING":
		    	$this->_CrashControl++;
		    	sleep(2);
		    	if($this->_CrashControl < $this->_MaxCrash){
			    	$this->openApplication();
			    	return false;
		    	}else{
		    		die("<p>ERROR!, Application crash!</p>")
		    	}
		    	break;
		    case "CREATED":
		    	$this->startApplication();
		    	break;
		    case "STARTED":
		    case "RUNING":
		    	$this->resumeApplication();
		    	break;
		    case "PAUSED":
		    case "STOPED":
		    	$this->restartApplication();
		    	break;
		   	case "DESTROYED":
		   		$this->createApplication();
        		break;
    		default:
    			die("<p>ERROR!, Status not registered: ".$_Status);
    			break;
        }
        return true;
    }

    /*
     * CREATE
     * @autor                           Albert Moreno Vendrell
     */
    private function createApplication(){
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "CREATING";

        $this->_instance->onCreate();

        $this->_TimeCreated		= time();
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "CREATED";
    }

    /*
     * START
     * @autor                           Albert Moreno Vendrell
     */
    private function startApplication(){
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "STARTING";

        $this->_instance->onStart();

        $this->_TimeStarted		= time();
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "STARTED";
    }

    /*
     * RESUME
     * @autor                           Albert Moreno Vendrell
     */
    private function resumeApplication(){
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "RUNING";
        $this->_instance->onResume();
    }

    /*
     * PAUSE
     * @autor                           Albert Moreno Vendrell
     */
    private function pauseApplication(){
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "PAUSING";

        $this->_instance->onStart();

        $this->_TimePaused		= time();
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "PAUSED";
    }

    /*
     * STOP
     * @autor                           Albert Moreno Vendrell
     */
    private function stopApplication(){
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "STOPING";

        $this->_instance->onStart();

        $this->_TimeStoped		= time();
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "STOPED";
    }

    /*
     * RESTART
     * @autor                           Albert Moreno Vendrell
     */
    private function restartApplication(){
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "RESTARTING";

        $this->_instance->onStart();

        $this->_TimeRestarted	= time();
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "RESTARTED";
    }

    /*
     * DESTROY
     * @autor                           Albert Moreno Vendrell
     */
    private function destroyApplication(){
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "DESTROYING";

        $this->_instance->onStart();

        $this->_TimeDestroyed	= time();
        $this->_TimeUpdated 	= time();
        $this->_Status      	= "DESTROYED";
    }
}